package com.character.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.character.entity.Log;
import com.character.entity.MyReturn;
import com.character.service.LogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Log)表控制层
 *
 * @author makejava
 * @since 2021-12-08 15:09:52
 */
@RestController
@RequestMapping("log")
public class LogController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private LogService logService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param log 查询实体
     * @return 所有数据
     */
    @GetMapping
    public MyReturn selectAll(Integer limit, Integer page, Log log) {
        Page<Log> logPage = new Page<>(page,limit);
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("operation_time");
        if(ObjectUtil.isNotEmpty(log.getUserName())){
            queryWrapper.like("user_name",log.getUserName());
        }
        if(ObjectUtil.isNotEmpty(log.getIp())){
            queryWrapper.like("ip",log.getIp());
        }
        Page<Log> page1 = this.logService.page(logPage,queryWrapper);
        return super.success(page1.getRecords(),page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.logService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param log 实体对象
     * @return 新增结果
     */
    @PostMapping
    public MyReturn insert(@RequestBody Log log) {
        return success(this.logService.save(log));
    }

    /**
     * 修改数据
     *
     * @param log 实体对象
     * @return 修改结果
     */
    @PutMapping
    public MyReturn update(@RequestBody Log log) {
        return success(this.logService.updateById(log));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public MyReturn delete(@RequestParam("idList") List<Long> idList) {
        return success(this.logService.removeByIds(idList));
    }
}

